package com.sl.thread.thread_print;

/**
 * Created by lisulong on 2019/11/1.
 */
public class ThreadPrintDemo0 {
    public static void main(String[] args) {
        final ThreadPrintDemo0 demo2 = new ThreadPrintDemo0();
        Thread t1 = new Thread(demo2::print1);//一对冒号的调用直接变为Runnable方法；
        Thread t2 = new Thread(demo2::print2);

        t1.start();
        t2.start();
    }

    public synchronized void print2() {
        for (int i = 1; i <= 100; i += 2) {
            System.out.println(i);
            this.notify();
            try {
                this.wait();
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }

    public synchronized void print1() {
        for (int i = 0; i <= 100; i += 2) {
            System.out.println(i);
            this.notify();
            try {
                this.wait();
                Thread.sleep(100);// 防止打印速度过快导致混乱
            } catch (InterruptedException e) {
                // NO
            }
        }
    }
}